#*******************************************************************************************
#
#   raylib [textures] example - Load textures from raw data
#
#   NOTE: Images are loaded in CPU memory (RAM) textures are loaded in GPU memory (VRAM)
#
#   This example has been created using raylib 1.3 (www.raylib.com)
#   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
#
#   Copyright (c) 2015 Ramon Santamaria (@raysan5)
#   /Converted in 2*20 by Guevara-chan.
#   Adapted in 2021 by greenfork
#
#*******************************************************************************************

import nimraylib_now

#  Initialization
# --------------------------------------------------------------------------------------
const screenWidth = 800
const screenHeight = 450

# Generate a checked texture by code
let
  width: int32 = 960
  height: int32 = 480

initWindow screenWidth, screenHeight, "raylib [textures] example - texture from raw data"

# NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)

# Load RAW image data (512x512, 32bit RGBA, no file header)
let
  fudesumiRaw = loadImageRaw("resources/fudesumi.raw", 384, 512, UNCOMPRESSED_R8G8B8A8, 0)
  fudesumi = loadTextureFromImage(fudesumiRaw)  #  Upload CPU (RAM) image to GPU (VRAM)
# unloadImage fudesumiRaw                           #  Unload CPU (RAM) image data

# Dynamic memory allocation to store pixels data (Color type)
var pixels = cast[ptr UncheckedArray[Color]](memAlloc(width*height * sizeof(Color)))

for y in 0..<height:
  for x in 0..<width:
    pixels[y*width + x] = (if (((x div 32+y div 32) div 1) %% 2 == 0): Orange else: Gold)

# Load pixels data into an image structure and create texture
let
  checkedIm = Image(
    data: pixels,
    width: width,
    height: height,
    format: UNCOMPRESSED_R8G8B8A8,
    mipmaps: 1
  )
  checked = loadTextureFromImage(checkedIm)
#UnloadImage checkedIm         #  Unload CPU (RAM) image data (do not do this in Nim, seriously)
# ---------------------------------------------------------------------------------------

#  Main game loop
while not windowShouldClose(): #  Detect window close button or ESC key
  #  Update
  # ----------------------------------------------------------------------------------
  #  TODO: Update your variables here
  # ----------------------------------------------------------------------------------

  #  Draw
  # ----------------------------------------------------------------------------------
  beginDrawing()

  clearBackground(RAYWHITE)

  drawTexture(
    checked,
    screenWidth div 2 - checked.width div 2,
    screenHeight div 2 - checked.height div 2,
    fade(White, 0.5f)
  )
  drawTexture fudesumi, 430, -30, White

  drawText "CHECKED TEXTURE ", 84, 85, 30, Brown
  drawText "GENERATED by CODE", 72, 148, 30, Brown
  drawText "and RAW IMAGE LOADING", 46, 210, 30, Brown

  drawText "(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, Brown

  endDrawing()
# ----------------------------------------------------------------------------------

#  De-Initialization
# --------------------------------------------------------------------------------------
unloadTexture fudesumi     #  Texture unloading
unloadTexture checked      #  Texture unloading

closeWindow()              #  Close window and OpenGL context
# --------------------------------------------------------------------------------------
